package com.onion.utils.excel;

import com.alibaba.fastjson.JSONObject;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ExcelUtils {

    private static final String KEY_DATA_LIST = "dataList";
    private static final String DEFAULT_TEMPLATE_PATH = "/template/excel/";


    /**
     * 生成Excel
     * @param template      Excel模板
     * @param dataParams    参数数据
     * @param dataList      列表数据
     * @param output        输出文件
     * @param <T>
     */
    public static <T> void genExcel(String template, Map<String,Object> dataParams, List<T> dataList, String output){
        String templateDir = System.getProperty("onion.template.excel.root", DEFAULT_TEMPLATE_PATH);
        InputStream fis = ExcelUtils.class.getResourceAsStream(templateDir + template);
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(output);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("FileNotFound:" + output);
        }
        try{
            genExcel(fis, dataParams, dataList, fos);
        }finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                }
            }
            if(fos != null){
                try {
                    fos.flush();
                } catch (IOException e) {
                }
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * 生成Excel
     * @param template      Excel模板输入流
     * @param dataParams    参数数据
     * @param dataList      列表数据
     * @param os            输出流
     * @param <T>
     */
    public static <T> void genExcel(InputStream template, Map<String, Object> dataParams, List<T> dataList, OutputStream os){
        Context context = new Context();
        if(dataParams!=null && dataParams.size()>0){
            dataParams.forEach((key, data)->{
                if(key.equals(KEY_DATA_LIST)){
                    throw new RuntimeException("参数包含保留key:"+KEY_DATA_LIST);
                }
                context.putVar(key, data);
            });
        }
        if(dataList!=null && dataList.size()>0){
            context.putVar(KEY_DATA_LIST, dataList);
        }
        try {
            JxlsHelper.getInstance().processTemplate(template, os, context);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args){
        List<JSONObject> jsonList = new ArrayList<>();
        JSONObject user1 = new JSONObject();
        user1.put("index", 1);
        user1.put("name", "名称");
        user1.put("birthday", "2021-01-02");
        user1.put("age", 30);
        user1.put("amount", 101);
        JSONObject user2 = new JSONObject();
        user2.put("index", 1);
        user2.put("name", "名称");
        user2.put("birthday", "2021-01-02");
        user2.put("age", 25);
        user2.put("amount", 1010);
        jsonList.add(user1);
        jsonList.add(user2);

        Map<String,Object> jsonParams = new JSONObject();
        jsonParams.put("title", "测试报表");
        jsonParams.put("creator", "侯立峰");
        jsonParams.put("createTime", new Date());
        ExcelUtils.genExcel("onion-test.xlsx", jsonParams, jsonList, "d:/data.xlsx");
    }

}